package com.xdxc.model.dto;

import com.baomidou.mybatisplus.annotation.TableField;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import com.xdxc.utils.CustomDeserializer;
import com.xdxc.utils.JsonTypeHandler;
import com.xdxc.utils.LongListToStringSerializer;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.beans.BeanUtils;

import javax.validation.constraints.NotNull;
import java.io.Serializable;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class UserAnswerDto implements Serializable {
    private static final long serialVersionUID = 1L;

    @JsonSerialize(using = ToStringSerializer.class)
    @NotNull(message = "试卷ID不能为空")
    private Long paperId;

    @JsonSerialize(using = ToStringSerializer.class)
    private Long quId;

    private Integer answerType;

    @TableField(typeHandler = JsonTypeHandler.class)
    @JsonDeserialize(using = CustomDeserializer.class)
    private List<Long> answerIds;

    private String answerText;

    @JsonSerialize(using = ToStringSerializer.class)
    private Long checkId;

    private Long userId;

    private int version;
    private String uuId;
    private Long examId;

    // 新增关键字段（用于消息排序和去重）
    private long bornTimestamp = System.currentTimeMillis();
    private String msgId;

    // 防御性复制方法
    public UserAnswerDto deepCopy() {
        UserAnswerDto copy = new UserAnswerDto();
        BeanUtils.copyProperties(this, copy);
        copy.setAnswerIds(this.answerIds != null ? new ArrayList<>(this.answerIds) : null);
        return copy;
    }

    // 复合键生成
    public String getCompositeKey() {
        return String.format("%d_%d_%d", quId, userId, paperId);
    }
}